<?php
//测试控制器类
class IndexControl extends Control{

	public function __init(){

		  $setup = include ROOT_PATH . "Data/setup.php";
    	$this->assign('setup',$setup);
	}

    function index(){
     if(is_file('../Data/lock.php')) die('请先删除Data/lock.php文件后进行安装');
    	session('step',1);
    	$this->display();
    }

   public function check(){
   		//处理步骤
   		if(session('step')<1){

   			go('index');
   		}
   		session('step', 2);


   		$gd = gd_info();

   		$system = array(
   			'phpos'			=>	PHP_OS,
   			'phpversion'	=>	PHP_VERSION,
   			'uploadsize'	=>	ini_get('upload_max_filesize'),

   			'gdversion'	=>	$gd['GD Version'],
   			'diskspace'	=>	get_size(disk_free_space('.'))
   			);
 		$this->assign('system',$system);

   		$this->display();
   }


   public function setconfig(){
   	//处理步骤
   		if(session('step')<2){

   			go('check');
   		}
   		session('step', 3);
   		
		$this->display();
   }
    public function install_db(){
    	session('admin',$_POST['admin']);
    	//写数据库配置项
    	$this->_write_config();
    	$this->display();
    }

    /**
     * [_write_config 写数据库配置项]
     * @return [type] [description]
     */
    private function _write_config(){

    	$data = var_export($_POST['db'],TRUE);
   		$str = <<<str
<?php
if (!defined("HDPHP_PATH"))exit('No direct script access allowed');
return $data;
str;
		file_put_contents('../Cms/Common/Config/dbConfig.php', $str);
    }

    /**
     * [createtable 安装库表，插入数据]
     * @return [type] [description]
     */
    public function createtable(){
    	$config = include '../Cms/Common/Config/dbConfig.php';
      // p($config);

    	//链接库
    	mysql_connect($config['DB_HOST'],$config['DB_USER'],$config['DB_PASSWORD']);
    	mysql_query('SET NAMES UTF8');
    	//创建库
    	mysql_query('CREATE DATABASE ' . $config['DB_DATABASE'] . ' CHARSET UTF8');
    	//选中库
    	mysql_select_db($config['DB_DATABASE']);
//     	Array
// (
//     [DB_HOST] => 127.0.0.1
//     [DB_USER] => root
//     [DB_PASSWORD] => 
//     [DB_DATABASE] => cms_edu
//     [DB_PORT] => 3306
//     [DB_PREFIX] => hd_
//     [DB_DRIVER] => mysqli
// )
// //引改SQL文件
    $sqlData = file_get_contents('./Data/cms_edu.sql');
    	//创建表
      $n = 0;
    if(preg_match_all("/CREATE\s+TABLE\s+`(.*?)`.*?;/is", $sqlData, $t)){
      foreach ($t[0] as $k=>$v) {
        $str = str_replace($t[1][$k], $config['DB_PREFIX'].$t[1][$k], $v);
       p($t[1][$n].'安装完成……');
        $n = $n + 1; 
        mysql_query($str);
      }
    }
    //插入表
    if(preg_match_all("/INSERT\s+INTO\s+`(.*?)`.*?;/is", $sqlData, $i)){
      foreach ($i[0] as $k=>$v) {
        $str = str_replace($i[1][$k], $config['DB_PREFIX'].$i[1][$k], $v);
        mysql_query($str);
      }
      //创建安装成功文件
      // 组合路径
      // $root =  dirname(__ROOT__); 
      $str = '您已装成功，如果需要重新安装，请删除此文件';
      file_put_contents('../Data/lock.php', $str);
      // $url ="ehco <script type='text/javascript'>top.location.href='*.php'</script>";;
      // $this->success('数据库安装成功',$url);
      // echo "Response.Write('<scriptwindow.open('{U|'Index/finish'}','');</script>')";
     $this->success('数据库安装成功',U('Index/finish'));
     }
    }
    /**
     * [finish 完成安装]
     * @return [type] [description]
     */
      public function finish(){
        // 获得键接
        $url = dirname(__ROOT__);
        $this->assign('url',$url);
      $this->display('finish.html');
    }
}
?>